package com.bestcem.xm.txtanls.controller;

import cn.hutool.core.lang.Validator;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.txtanls.controller.convert.ChartConvert;
import com.bestcem.xm.txtanls.controller.param.chart.*;
import com.bestcem.xm.txtanls.controller.vo.chart.*;
import com.bestcem.xm.txtanls.grpc.service.param.chart.*;
import com.bestcem.xm.txtanls.service.ChartService;
import com.bestcem.xm.txtanls.service.dto.chart.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * 文本分析图表查询
 * @author jy.zhao
 * @version 2.0
 * @date 2021/8/5 17:19
 **/
@Api(tags = "文本分析图表查询接口")
@RestController
@RequestMapping(value = "/txtanls/charts", produces = MediaType.APPLICATION_JSON_VALUE)
public class ChartController {

    @Autowired
    private ChartService chartService;

    @ApiOperation(value = "总体情感细分查询")
    @GetMapping(value = "/emotion_detail")
    public XmResultJson<EmotionDetailVO> emotionDetail(@RequestParam(value = "project_id") String projectId,
                                                       @RequestParam(value = "field_id", required = false) String fieldId,
                                                       @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                       @RequestParam(value = "current", required = false) String current,
                                                       @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                       @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        BaseChartQuery baseChartQuery = new BaseChartQuery(projectId, fieldId, filterType, current, startTime, endTime);
        BaseChartParam baseChartParam = baseChartQuery.getBaseChartParam();
        String errorMessage = ChartValid.validBaseChartParam(baseChartParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<EmotionDetailDTO> serviceResult = chartService.statEmotion(baseChartParam);
        EmotionDetailVO emotionDetailVO = ChartConvert.emotionDetailDTOToVO(serviceResult.getData(), baseChartQuery.getInterval().getIndex());
        return XmResultJson.success(emotionDetailVO);
    }

    @ApiOperation(value = "总体情感趋势查询")
    @GetMapping(value = "/emotion_trend")
    public XmResultJson<List<EmotionTrendVO>> emotionTrend(@RequestParam(value = "project_id") String projectId,
                                                           @RequestParam(value = "field_id", required = false) String fieldId,
                                                           @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                           @RequestParam(value = "current", required = false) String current,
                                                           @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                           @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        BaseChartQuery baseChartQuery = new BaseChartQuery(projectId, fieldId, filterType, current, startTime, endTime);
        BaseChartParam baseChartParam = baseChartQuery.getBaseChartParam();
        String errorMessage = ChartValid.validBaseChartParam(baseChartParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<List<EmotionDTO>> serviceResult = chartService.statEmotionTrend(baseChartParam);
        List<EmotionTrendVO> emotionTrendVOList = ChartConvert.emotionDTOToVOList(serviceResult.getData(), baseChartQuery.getInterval().getIndex());
        return XmResultJson.success(emotionTrendVOList);
    }

    @ApiOperation(value = "观点统计")
    @GetMapping(value = "/viewpoint_stat")
    public XmResultJson<List<ViewpointGroupStatVO>> viewpointStat(@RequestParam(value = "project_id") String projectId,
                                                                  @RequestParam(value = "field_id", required = false) String fieldId,
                                                                  @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                                  @RequestParam(value = "current", required = false) String current,
                                                                  @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                                  @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        BaseChartQuery baseChartQuery = new BaseChartQuery(projectId, fieldId, filterType, current, startTime, endTime);
        BaseChartParam baseChartParam = baseChartQuery.getBaseChartParam();
        String errorMessage = ChartValid.validBaseChartParam(baseChartParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<ViewpointStatResultDTO> serviceResult = chartService.statViewpoint(baseChartParam);
        List<ViewpointGroupStatVO> viewpointGroupStatVOList = ChartConvert.ViewpointGroupStatDTOToVOList(serviceResult.getData().getViewPointGroupStatDTOS());
        return XmResultJson.success(viewpointGroupStatVOList);
    }

    @ApiOperation(value = "观点top5")
    @GetMapping(value = "/viewpoint_top")
    public XmResultJson<ViewpointTopDTO> viewpointTop(@RequestParam(value = "project_id") String projectId,
                                                      @RequestParam(value = "field_id", required = false) String fieldId,
                                                      @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                      @RequestParam(value = "current", required = false) String current,
                                                      @RequestParam(value = "period", required = true) Integer period,
                                                      @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                      @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime) {
        PeriodChartQuery periodChartQuery = new PeriodChartQuery(projectId, fieldId, filterType, current, startTime, endTime, period);
        PeriodChartParam periodChartParam = periodChartQuery.getPeriodChartParam();
        String errorMessage = ChartValid.validViewpointEvaluationTopParam(periodChartParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<ViewpointTopDTO> serviceResult = chartService.viewpointTop(periodChartParam);
        return XmResultJson.success(serviceResult.getData());
    }

    @ApiOperation(value = "话题提及率变化")
    @GetMapping(value = "/topic_reference")
    public XmResultJson<TopicReferenceRateVO> topicReference(@RequestParam(value = "project_id") String projectId,
                                                             @RequestParam(value = "field_id", required = false) String fieldId,
                                                             @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                             @RequestParam(value = "current", required = false) String current,
                                                             @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                             @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                                             @RequestParam(value = "topics_group_id", required = false) String topicGid,
                                                             @RequestParam(value = "sort", required = false) String sortKey,
                                                             @RequestParam(value = "topics_type", required = false) String topicType) {

        TopicReferenceQuery topicReferenceQuery = new TopicReferenceQuery(projectId, fieldId, filterType, current, startTime, endTime, topicGid, topicType, sortKey);
        TopicReferenceParam topicReferenceParam = topicReferenceQuery.getTopicReferenceParam();
        String errorMessage = ChartValid.validTopicReferenceParam(topicReferenceParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<TopicReferenceRateDTO> serviceResult = chartService.statTopicReference(topicReferenceParam);
        TopicReferenceRateVO vo = ChartConvert.topicReferenceRateDTOToVO(serviceResult.getData(), topicReferenceQuery.getInterval().getIndex());
        return XmResultJson.success(vo);
    }

    @ApiOperation(value = "话题提及率变化")
    @GetMapping(value = "/topic_analysis")
    public XmResultJson<TopicAnalysisVO> topicAnalysis(@RequestParam(value = "project_id") String projectId,
                                                       @RequestParam(value = "field_id", required = false) String fieldId,
                                                       @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                       @RequestParam(value = "current", required = false) String current,
                                                       @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                       @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                                       @RequestParam(value = "period", required = false) Integer period) {

        PeriodChartQuery periodChartQuery = new PeriodChartQuery(projectId, fieldId, filterType, current, startTime, endTime, period);
        PeriodChartParam periodChartParam = periodChartQuery.getPeriodChartParam();
        String errorMessage = ChartValid.validViewpointEvaluationTopParam(periodChartParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<TopicAnalysisDTO> serviceResult = chartService.statTopicAnalysis(periodChartParam);
        TopicAnalysisVO vo = ChartConvert.topicAnalysisDTOToVO(serviceResult.getData());
        return XmResultJson.success(vo);
    }

    @ApiOperation(value = "观点评价变化")
    @GetMapping(value = "/viewpoint_evaluate")
    public XmResultJson<ViewPointEvaluateVO> viewpointEvaluate(@RequestParam(value = "project_id") String projectId,
                                                               @RequestParam(value = "field_id", required = false) String fieldId,
                                                               @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                               @RequestParam(value = "current", required = false) String current,
                                                               @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                               @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                                               @RequestParam(value = "view_group_id", required = false) String viewpointGid,
                                                               @RequestParam(value = "sort", required = false) String sortKey,
                                                               @RequestParam(value = "view_type", required = false) String viewpointType,
                                                               @RequestParam(value = "comment_type", required = false) String evaluationType) {
        ViewpointEvaluateQuery viewpointEvaluateQuery = new ViewpointEvaluateQuery(projectId, fieldId, filterType, current, startTime, endTime, viewpointGid, sortKey, viewpointType, evaluationType);
        ViewpointEvaluateParam viewpointEvaluateParam = viewpointEvaluateQuery.getViewpointEvaluateParam();
        String errorMessage = ChartValid.validViewpointEvaluateParam(viewpointEvaluateParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<ViewPointEvaluateDTO> serviceResult = chartService.statViewpointEvaluate(viewpointEvaluateParam);
        ViewPointEvaluateVO vo = ChartConvert.ViewPointEvaluateDTOToVO(serviceResult.getData(), viewpointEvaluateQuery.getInterval().getIndex());
        return XmResultJson.success(vo);
    }

    @ApiOperation(value = "词云图")
    @GetMapping(value = "/word_cloud")
    public XmResultJson<List<WorkCloudVO>> wordCloud(@RequestParam(value = "project_id") String projectId,
                                                     @RequestParam(value = "field_id", required = false) String fieldId,
                                                     @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                     @RequestParam(value = "current", required = false) String current,
                                                     @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                     @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                                     @RequestParam(value = "topicId", required = false) String topicId) {
        WorkCloudQuery workCloudQuery = new WorkCloudQuery(projectId, fieldId, filterType, current, startTime, endTime, topicId);
        WorkCloudParam workCloudParam = workCloudQuery.getWorkCloudParam();
        String errorMessage = ChartValid.validWorkCloudParam(workCloudParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<List<WorkCloudDataDTO>> serviceResult = chartService.statKeyword(workCloudParam);
        List<WorkCloudVO> workCloudVOList = ChartConvert.workCloudDataDTOToVOList(serviceResult.getData());
        return XmResultJson.success(workCloudVOList);
    }

    @ApiOperation(value = "话题关联分析")
    @GetMapping(value = "/topic_relation")
    public XmResultJson<List<TopicRelationVO>> topicRelation(@RequestParam(value = "project_id") String projectId,
                                                             @RequestParam(value = "field_id", required = false) String fieldId,
                                                             @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                             @RequestParam(value = "current", required = false) String current,
                                                             @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                             @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                                             @RequestParam(value = "topic_id", required = false) String topicId) {
        TopicRelationQuery topicRelationQuery = new TopicRelationQuery(projectId, fieldId, filterType, current, startTime, endTime, topicId);
        TopicRelationParam topicRelationParam = topicRelationQuery.getTopicRelationParam();
        String errorMessage = ChartValid.validTopicRelationParam(topicRelationParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<List<TopicRelationDTO>> serviceResult = chartService.statTopicRelation(topicRelationParam);
        List<TopicRelationVO> topicRelationVOList = ChartConvert.topicRelationDTOToVOList(serviceResult.getData());
        return XmResultJson.success(topicRelationVOList);
    }

    @ApiOperation(value = "话题提及率变化趋势")
    @GetMapping(value = "/topic_reference_trend")
    public XmResultJson<List<TopicStatVO>> topicReferenceTrend(@RequestParam(value = "project_id") String projectId,
                                                               @RequestParam(value = "field_id", required = false) String fieldId,
                                                               @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                               @RequestParam(value = "current", required = false) String current,
                                                               @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                               @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                                               @RequestParam(value = "topics_group_id", required = false) String topicGid,
                                                               @RequestParam(value = "topics_id", required = false) String topicId) {
        TopicReferenceTrendQuery topicReferenceTrendQuery = new TopicReferenceTrendQuery(projectId, fieldId, filterType, current, startTime, endTime, topicGid, topicId);
        TopicReferenceTrendParam topicReferenceTrendParam = topicReferenceTrendQuery.getTopicReferenceTrendParam();
        String errorMessage = ChartValid.validTopicReferenceTrendParam(topicReferenceTrendParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<List<TopicStatDTO>> serviceResult = chartService.statTopicReferenceTrend(topicReferenceTrendParam);
        List<TopicStatVO> statVOList = ChartConvert.topicStatDTOToVOList(serviceResult.getData(), topicReferenceTrendQuery.getInterval().getIndex());
        return XmResultJson.success(statVOList);
    }

    @ApiOperation(value = "观点情感分布变化")
    @GetMapping(value = "/viewpoint_evaluate_trend")
    public XmResultJson<List<ViewpointEvaluateStatVO>> viewpointEvaluateTrend(@RequestParam(value = "project_id") String projectId,
                                                                              @RequestParam(value = "field_id", required = false) String fieldId,
                                                                              @RequestParam(value = "dtfiltertype", required = false) String filterType,
                                                                              @RequestParam(value = "current", required = false) String current,
                                                                              @RequestParam(value = "start_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                                                              @RequestParam(value = "end_time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                                                              @RequestParam(value = "view_id", required = false) String viewId,
                                                                              @RequestParam(value = "view_group_id", required = false) String viewGid) {
        ViewpointEvaluateTrendQuery viewpointEvaluateTrendQuery = new ViewpointEvaluateTrendQuery(projectId, fieldId, filterType, current, startTime, endTime, viewGid, viewId);
        ViewpointEvaluateTrendParam viewpointEvaluateTrendParam = viewpointEvaluateTrendQuery.getViewpointEvaluateTrendParam();
        String errorMessage = ChartValid.validViewpointEvaluateTrendParam(viewpointEvaluateTrendParam);
        if(Validator.isNotNull(errorMessage)) {
            return XmResultJson.fail(5, errorMessage);
        }
        ServiceResult<List<ViewpointEvaluateStatDTO>> serviceResult = chartService.statViewpointEvaluateTrend(viewpointEvaluateTrendParam);
        List<ViewpointEvaluateStatVO> viewpointEvaluateStatVOList = ChartConvert.viewpointEvaluateStatDTOToVOList(serviceResult.getData(), viewpointEvaluateTrendQuery.getInterval().getIndex());
        return XmResultJson.success(viewpointEvaluateStatVOList);
    }
}